package com.spring.boot.test.config.stragegy;

import java.util.ArrayList;
import java.util.List;

public class CommonShardingAlgorithm {
    protected final static List<FieldShardStrategy> fieldShardStrategyList;

    static {
        List<FieldShardStrategy> tmp = new ArrayList<>();
        tmp.add(new PayNoDbShardStrategy());
        tmp.add(new PayNoTableShardStrategy());
        tmp.add(new BusinessNoDbShardStrategy());
        tmp.add(new BusinessNoTableShardStrategy());
        fieldShardStrategyList = tmp;
    }

    protected String getFieldShardStrategy(String fieldName,String fieldValue, ShardStragegyTypeEnum shardStragegyTypeEnum) {
        TableFieldEnum tableFieldEnum = TableFieldEnum.getByFieldName(fieldName);
        if (null != tableFieldEnum) {
            for (FieldShardStrategy fieldShardStrategy : fieldShardStrategyList) {
                if (fieldShardStrategy.shardStragegyTypeEnum() != null
                        && fieldShardStrategy.shardStragegyTypeEnum().equals(shardStragegyTypeEnum)
                        && tableFieldEnum.equals(fieldShardStrategy.tableFieldEnum())) {
                    return fieldShardStrategy.getShardSuffix(fieldValue);
                }
            }
        }
        throw new UnsupportedOperationException();
    }
}
